﻿/********************************************************************************************
 * Time de desenvolvedores
 *  Vinícius Batista de Souza       @vbs_br
 *  Fermando Martin Figuera         @fernandomartin
 *  
 *  
 * Jogo desenvolvido para testar algumas features e produzir trilha de treinamento no MSDN.
 * Faça o download do SDK do Windows Phone, faça você também o seu jogo :)
 * 
 * Dúvidas utilizem o Forúm do Windows Phone no MSDN.
********************************************************************************************/




using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Threading;

namespace ComboBUG
{
    public partial class ScorePage : PhoneApplicationPage
    {

        Helpers.FacebookShare facebookShare;
        DispatcherTimer time_button_share = new DispatcherTimer();

        #region ScorePage
        public ScorePage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ScorePage_Loaded);
            NewGame.Click += new RoutedEventHandler(NewGame_Click);
            btShare.Click += new RoutedEventHandler(btShare_Click);
        }
        #endregion

        #region ScorePage_Loaded
        void ScorePage_Loaded(object sender, RoutedEventArgs e)
        {
            btShare.Content = "Share your score on Facebook";
            btShare.IsEnabled = true;

            IsolatedStorageSettings settings =
                IsolatedStorageSettings.ApplicationSettings;

            string value = "0";
            string level = "0";

            bool error = false;

            try
            {
                settings.TryGetValue("maxScore", out value);

                lblScore.Text = value;
            }
            catch
            {
                error = true;
            }

            try
            {
                if (error) throw new Exception("Error above, prevent this to be executed!");
                settings.TryGetValue("maxLevel", out level);

                lblLevel.Text = level;
            }
            catch
            {
                error = true;
            }

            if (error || (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(level)))
            {
                lblLevel.Visibility = Visibility.Collapsed;
                lblScore.Visibility = Visibility.Collapsed;
                labelLevel.Visibility = Visibility.Collapsed;
                btShare.Visibility = Visibility.Collapsed;
            }
            else
            {
                lblLevel.Visibility = Visibility.Visible;
                lblScore.Visibility = Visibility.Visible;
                labelLevel.Visibility = Visibility.Visible;
                btShare.Visibility = Visibility.Visible;
            }

        }
        #endregion

        #region btShare_Click
        void btShare_Click(object sender, RoutedEventArgs e)
        {
            lblLevel.Visibility = Visibility.Collapsed;
            lblScore.Visibility = Visibility.Collapsed;
            labelLevel.Visibility = Visibility.Collapsed;
            //btShare.Visibility = Visibility.Collapsed;
            NewGame.Visibility = Visibility.Collapsed;

            if (btShare.Content.ToString().ToLower().IndexOf("share") != -1)
            {

                btShare.Content = "Processing...";

                time_button_share.Interval = TimeSpan.FromMilliseconds(250);
                time_button_share.Tick += new EventHandler(time_button_share_Tick);
                time_button_share.Start();

                string shareDescription = string.Format(Helper.shareDescription, lblScore.Text, lblLevel.Text, string.Empty, Helper.urlCentrodeTreinamento);
                facebookShare = new Helpers.FacebookShare(browserAuth, Helper.linkToShare, Helper.shareTitle, shareDescription, Helper.shareImage);

            }
        }
        #endregion

        #region time_button_share_Tick
        void time_button_share_Tick(object sender, EventArgs e)
        {
            if (facebookShare.isCompleted)
            {
                btShare.Content = "Completed with success!";
                time_button_share.Stop();
            }

            if (facebookShare.timeout)
            {
                btShare.Content = "Share your score on Facebook";
                time_button_share.Stop();
                MessageBox.Show("Sorry! Facebook's server is not available, check your internet connection and try again!");
            }

            if (facebookShare.isCompleted || facebookShare.timeout)
            {
                lblLevel.Visibility = Visibility.Visible;
                lblScore.Visibility = Visibility.Visible;
                labelLevel.Visibility = Visibility.Visible;
                btShare.Visibility = Visibility.Visible;
                NewGame.Visibility = Visibility.Collapsed;
            }

            
        }
        #endregion

        #region NewGame_Click
        void NewGame_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }
        #endregion

        #region OnBackKeyPress

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

            NavigationService.Navigate(new Uri(string.Format("/MainPage.xaml"), UriKind.Relative));

        }
        #endregion


    }
}